import 'package:flutter/material.dart';
import 'package:photo_kit/photo_asset.dart';
import 'package:photo_kit/photo_data_provider.dart';
import 'package:photo_kit/photo_kit.dart';
import 'package:photo_kit_example/album_photo_page.dart';

class AlbumDataPhotoPage extends StatefulWidget {
  AlbumDataPhotoPage({Key key}) : super(key: key);

  @override
  _AlbumDataPhotoPageState createState() => _AlbumDataPhotoPageState();
}

class _AlbumDataPhotoPageState extends State<AlbumDataPhotoPage> {
  List<PhotoAsset> photos = [];
  String title = 'Photos';

  @override
  void initState() {
    super.initState();

    _reqeustPhotos();
  }

  Future _reqeustPhotos() async {
    photos = await PhotoKit.getAllPhoto();
    title = 'All Photo ${photos.length}';
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return AlbumPhotoPage(
      title: title,
      itemCount: photos.length,
      itemBuilder: (context, index) {
        final photo = photos[index];
        if (photo is! PhotoAsset) return Container();

        return _ItemWidget(photo: photo);
      },
    );
  }
}

class _ItemWidget extends StatelessWidget {
  const _ItemWidget({
    Key key,
    @required this.photo,
  }) : super(key: key);

  final PhotoAsset photo;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.black,
          width: 1,
        ),
        color: Colors.black12,
      ),
      child: Image(
        image: PhotoDataProvider(
          photo: photo,
          width: 200,
        ),
      ),
    );
  }
}
